package com.woyou.ui.component;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.citaq.ideliver.R;
import com.woyou.utils.eventbus.EventRemark;

import de.greenrobot.event.EventBus;

/**
 * * UI 工具组件
 * 
 * @author 荣
 * 
 */
public class SuperUI {
	
	static Dialog dialog;
	
	/**
	 * 通用的底部Dialog
	 */
	public static Dialog openBottomDialog(Context context, String message, String left, OnClickListener leftListener, String right, OnClickListener rightListener) {
		dialog = new Dialog(context, R.style.defaultDialogStyle);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.BOTTOM);
		//设置dialog平铺
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
        
		View view = LayoutInflater.from(context).inflate(R.layout.custom_shoppingcar_clear, null);
		RelativeLayout layout=(RelativeLayout)view.findViewById(R.id.custom_layout);
		Button rightBtn = (Button) view.findViewById(R.id.ok_button);
		Button leftBtn = (Button) view.findViewById(R.id.cancel_button);
		TextView msg = (TextView) view.findViewById(R.id.msg);
		msg.setText(message);
		msg.setOnClickListener(null);
		if( !TextUtils.isEmpty(left) ){
			leftBtn.setText(left);
		}
        if( !TextUtils.isEmpty(right) ){
        	rightBtn.setText(right);
		}
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		rightBtn.setOnClickListener(rightListener);
		leftBtn.setOnClickListener(leftListener);
		return dialog;

	}
	

	/**
	 * 打开确认订单的dialog
	 * @param context
	 */
	public static void openRemarkDialog(Context context) {
		final Dialog dialog = new Dialog(context, R.style.defaultDialogStyle);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.BOTTOM);
		//设置dialog平铺
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);

		View view = LayoutInflater.from(context).inflate(R.layout.custom_remark, null);
		Button ok = (Button) view.findViewById(R.id.ok_button);
		Button cancel = (Button) view.findViewById(R.id.cancel_button);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();

		ok.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(new EventRemark());
				dialog.dismiss();
			}
		});
		cancel.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

	}
	/**
	 * 打开推送消息的的dialog
	 * @param mActivity
	 */
	public static void openPullMessageDialog(Activity mActivity,String info) {
		final Dialog dialog = new Dialog(mActivity, R.style.defaultDialogStyle);
		//设置dialog平铺
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;
		win.setAttributes(lp);

		View view = LayoutInflater.from(mActivity).inflate(R.layout.custom_pullmessage, null);
		TextView tv = (TextView) view.findViewById(R.id.custom_pullmessage_tv);
		Button b=(Button)view.findViewById(R.id.custom_pullmessage_b);
		tv.setText(""+info);

		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();

		b.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

	}

	public static void dismiss() {
		if(dialog!=null )
		   dialog.dismiss();
		
	}
	/**
	 * 显示收藏成功UI
	 */
	public static void showCollectionUI(Context context){
		View view = LayoutInflater.from(context).inflate(R.layout.custom_collection, null);
		Toast toast=new Toast(context);
		toast.setView(view);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	/**
	 * 显示取消收藏成功UI
	 */
	public static void showUncollectionUI(Context context){
		View view = LayoutInflater.from(context).inflate(R.layout.custom_uncollection, null);
		Toast toast=new Toast(context);
		toast.setView(view);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
}
